#!/bin/bash
#PBS -l nodes=1:ppn=16
#PBS -o ./out.$PBS_JOBID
#PBS -e ./error.$PBS_JOBID

logID=000000
config=~/.wsconfig
baseJobName=`grep "baseJobName" $config | cut -d " " -f 3`
root_dir=`grep "root_dir" $config | cut -d " " -f 3`
logFile=$root_dir/log/${logID}.log

if [ "$PBS_O_WORKDIR" = "" ];then
    PBS_O_WORKDIR=`pwd`
fi

if [ "$PBS_JOBID" = "" ];then
    PBS_JOBID=$1
    touch out.$PBS_JOBID
fi

jobID=`echo "${PBS_JOBID}" | cut -d . -f 1` 

cd $PBS_O_WORKDIR

resultDir=res.$jobID
if [ "$2" = "" ];then
	ctrlFile=input/Copper.ctrl
else
	ctrlFile=$2
fi
dataFile=input/Copper.data

dirName=`pwd | sed 's,^\(.*/\)\?\([^/]*\),\2,'`
echo  "$dirName, jobID $jobID:" >> $logFile

if [ $PBS_JOBID = $1 ]
then
#   make clean  > /dev/null 2>&1
#   make  > /dev/null 2>&1
    resultDir=res.$jobID
    lineNumber=`grep  -n "dirname" $ctrlFile | cut -d : -f 1`
    sed -i "${lineNumber}d" $ctrlFile
    sed -i "${lineNumber}idirname = \"${resultDir}\"" $ctrlFile

    mpirun -n 4 ./bin/paradis -n 1 ./${ctrlFile} | tee out.${jobID}
else
#	make clean  > /dev/null 2>&1
#    	make  > /dev/null 2>&1
#	echo "Initial generation type: ${initialType}"
#	./gen 
        lineNumber=`grep  -n "numZdoms" $ctrlFile | cut -d : -f 1`
        sed -i "${lineNumber}d" $ctrlFile
        sed -i "${lineNumber}inumZdoms = 4" $ctrlFile
    	mpirun -machinefile $PBS_NODEFILE -n 16 ./bin/paradis -n 1 ./${ctrlFile}
fi
